﻿using _8Group.MES.MeasuringUnit;
using _8Group.MES.MeasuringUnit.DTO;
using _8Group.MES.Model.MD主数据;
using _8Group.MES.Model.PRO生产管理;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace _8Group.MES.WorkStation
{
    [ApiExplorerSettings(GroupName = "李明豫")]
    public class WorkStationService:ApplicationService,IWorkStationService
    {
        private readonly IRepository<MdWorkstation, int> _workStationRepository;
        private readonly IRepository<MdWorkshop, int> _workshopRepository;
        private readonly IRepository<Pro_Process, int> _processRepository;
        public WorkStationService(IRepository<MdWorkstation, int> workStationRepository, IRepository<Pro_Process, int> processRepository, IRepository<MdWorkshop, int> workshopRepository)
        {
            _workStationRepository = workStationRepository;
            _processRepository = processRepository;
            _workshopRepository = workshopRepository;
        }

        /// <summary>
        /// 获取车间列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<MdWorkshop>> GetWorkShopList()
        {
            var workShopList = await _workshopRepository.GetListAsync();
            return workShopList;
        }

        /// <summary>
        /// 获取生产工序
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetProcessList()
        {
            var processList = await _processRepository.GetListAsync();
            return JsonConvert.SerializeObject(new
            {
                code = "0",
                data = processList,
                count = processList.Count(),
                msg = "success"
            }, Formatting.Indented);
        }

        /// <summary>
        /// 获取工作站列表
        /// </summary>
        /// <param name="workstationCode"></param>
        /// <param name="workstationName"></param>
        /// <param name="workshopID"></param>
        /// <param name="processID"></param>
        /// <returns></returns>
        public async Task<ResultResturn<List<WorkStationListDTO>>> GetWorkStationList(string? workstationCode, string? workstationName, int? workshopID, int? processID, int pageIndex, int pageSize)
        {
            var workStationList = await _workStationRepository.GetListAsync();
            if (!string.IsNullOrEmpty(workstationCode))
            {
                workStationList = workStationList.Where(m => m.WorkstationCode.Contains(workstationCode)).ToList();
            }
            if (!string.IsNullOrEmpty(workstationName))
            {
                workStationList = workStationList.Where(m => m.WorkstationName.Contains(workstationName)).ToList();
            }
            if (workshopID > 0)
            {
                workStationList = workStationList.Where(m => m.WorkshopID==workshopID).ToList();
            }
            if (processID > 0)
            {
                workStationList = workStationList.Where(m => m.ProcessID == processID).ToList();
            }
            var recordCount= workStationList.Count();
            workStationList = workStationList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            //var result=ObjectMapper.Map<List<MdWorkstation>, List<WorkStationListDTO>>(workStationList);
            var list = AutoMapperExt.MapToList<WorkStationListDTO>(workStationList);
            if (list != null)
            {
                return new ResultResturn<List<WorkStationListDTO>> { code = "0", data = list, count = recordCount, msg = "success" };
            }
            return new ResultResturn<List<WorkStationListDTO>> { code = "1", data = null, count = 0, msg = "获取失败" };
        }
        /// <summary>
        /// 添加工作站
        /// </summary>
        /// <param name="workStationAddDTO"></param>
        /// <returns></returns>
        public async Task<WorkStationAddDTO> AddWorkStation(WorkStationAddDTO workStationAddDTO)
        {
            var workStation = ObjectMapper.Map<WorkStationAddDTO, MdWorkstation>(workStationAddDTO);
            workStation.CreateBy = "李明豫";
            workStation.CreateTime = DateTime.Now;
            await _workStationRepository.InsertAsync(workStation);
            return ObjectMapper.Map< MdWorkstation, WorkStationAddDTO>(workStation);
        }
        /// <summary>
        /// 修改工作站
        /// </summary>
        /// <param name="workStationUpdateDTO"></param>
        /// <returns></returns>
        public async Task<WorkStationUpdateDTO> UpdateWorkStation(WorkStationUpdateDTO workStationUpdateDTO)
        {
            workStationUpdateDTO.UpdateBy = "李明豫";
            workStationUpdateDTO.UpdateTime = DateTime.Now;
            var workStation=AutoMapperExt.MapTo<MdWorkstation>(workStationUpdateDTO);
            await _workStationRepository.UpdateAsync(workStation);
            return workStationUpdateDTO;
        }

        /// <summary>
        /// 删除工作站
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteWorkStation(int id)
        {
            var workStation = await _workStationRepository.GetAsync(id);
            await _workStationRepository.DeleteAsync(workStation);
            return 1;
        }

    }
}
